import cv2
import os
import numpy as np
import copy
import random
def start_deal_00(start, end, new_start, path, save_path):  #处理00 ，0%, 8%
    all_imgs = []
    for i in range(start, end+1):

        left_img_path = os.path.join(path, str(i) + '_0.png')
        right_img_path = os.path.join(path, str(i) + '_1.png')
        left_img = cv2.imread(left_img_path,0)
        right_img = cv2.imread(right_img_path,0)
        # save = concat_img_s(left_img, right_img,0)
        # cv2.imwrite(os.path.join(save_path,str(i)) + '.png', save)

        for k in range(0, 2):  #字符宽度
            new_img = concat_img(left_img, np.zeros((left_img.shape[0],k), dtype=np.uint8))
            save_img = concat_img_s(new_img, right_img)
            cv2.imwrite(os.path.join(save_path, str(i))+ '_' + str(k) + '_' + '.png', save_img)

            new_img = add_v(save_img, 1, False)
            save_imgpp = add_v(new_img, 1, True)
            si = add_noise(save_imgpp, 5)
            cv2.imwrite(os.path.join(save_path, str(i) + '_' + str(k) + '_0a.png'), si)
            si = remove_noise(save_imgpp, 5)
            cv2.imwrite(os.path.join(save_path, str(i) + '_' + str(k) + '_0r.png'), si)

            new_img = add_v(save_img, 3, False)
            save_imgpp = add_v(new_img, 1, True)
            si = add_noise(save_imgpp, 5)
            cv2.imwrite(os.path.join(save_path, str(i) + '_' + str(k) + '_1a.png'), si)
            si = remove_noise(save_imgpp, 5)
            cv2.imwrite(os.path.join(save_path, str(i) + '_' + str(k) + '_1r.png'), si)

            new_img = add_v(save_img, 4, False)
            save_imgpp = add_v(new_img, 0, True)
            si = add_noise(save_imgpp, 5)
            cv2.imwrite(os.path.join(save_path, str(i) + '_' + str(k) + '_2a.png'), si)
            si = remove_noise(save_imgpp, 5)
            cv2.imwrite(os.path.join(save_path, str(i) + '_' + str(k) + '_2r.png'), si)

            new_img = add_v(save_img, 2, False)
            save_imgpp = add_v(new_img, 0, True)
            si = add_noise(save_imgpp, 5)
            cv2.imwrite(os.path.join(save_path, str(i) + '_' + str(k) + '_3a.png'), si)
            si = remove_noise(save_imgpp, 5)
            cv2.imwrite(os.path.join(save_path, str(i) + '_' + str(k) + '_3r.png'), si)

def start_deal_s(start, end, new_start, path, save_path):   #普通连接方式
    all_imgs = []
    for i in range(start, end+1):

        left_img_path = os.path.join(path, str(i) + '_0.png')
        right_img_path = os.path.join(path, str(i) + '_1.png')
        left_img = cv2.imread(left_img_path,0)
        right_img = cv2.imread(right_img_path,0)
        # save = concat_img_s(left_img, right_img,0)
        # cv2.imwrite(os.path.join(save_path,str(i)) + '.png', save)

        for k in range(1, 3):  #字符宽度
            new_img = concat_img(left_img, np.zeros((left_img.shape[0],k), dtype=np.uint8))

            for n in range(1, 7):
                save_img = concat_img_s(new_img, right_img, n)
                cv2.imwrite(os.path.join(save_path, str(i))+ '_' + str(k)+ '_' + str(k) + '.png', save_img)


def start_deal_s_t(start, end, new_start, path, save_path):   #普通连接方式
    all_imgs = []
    for i in range(start, end+1):

        left_img_path = os.path.join(path, str(i) + '_0.png')
        right_img_path = os.path.join(path, str(i) + '_1.png')
        left_img = cv2.imread(left_img_path,0)
        right_img = cv2.imread(right_img_path,0)
        # save = concat_img_s(left_img, right_img,0)
        # cv2.imwrite(os.path.join(save_path,str(i)) + '.png', save)

        for k in range(1, 4):  #字符宽度
            new_img = concat_img(left_img, np.zeros((left_img.shape[0],k), dtype=np.uint8))

            save_img = concat_img(new_img, right_img)
            cv2.imwrite(os.path.join(save_path, str(i))+ '_' + str(k) + '_' + '.png', save_img)




def concat_img_s(src1,src2,std_diff=1): # std_diff 为两字字符上下差  g)   连接两张图片
    diff = src1.shape[0]- src2.shape[0]
    s1 = np.asarray(src1,dtype=np.uint8).tolist()
    s2 = np.asarray(src2,dtype=np.uint8).tolist()

    if diff == 0:
        return concat(src1,src2)
    if diff >0:
        new_s2 = []
        new_s1 = []

        for k in range(len(s1)):
            new_s1.append(s1[k])



        # new_s2
        for k in range(len(new_s1) - len(s2)):
            t_l = []
            for i in range(len(s2[0])):
                t_l.append(0)
            new_s2.append(t_l)

        for k in range(len(s2)):
            new_s2.append(s2[k])



        return concat(np.asarray(new_s1, dtype=np.uint8), np.asarray(new_s2, dtype=np.uint8))
    else:
        new_s2 = []
        new_s1 = []

        for k in range(len(s2)):
            new_s2.append(s2[k])

        # new_s1
        for k in range(len(new_s2) - len(s1)):
            t_l = []
            for i in range(len(s1[0])):
                t_l.append(0)
            new_s1.append(t_l)

        for k in range(len(s1)):
            new_s1.append(s1[k])

        return concat(np.asarray(new_s1, dtype=np.uint8), np.asarray(new_s2, dtype=np.uint8))


def dilade_img(start, end, new_start, path, save_path):  #以家噪声的方式扩充图片

    for i in range(start, end+1):

        left_img_path = os.path.join(path, str(i) + '_0.png')

        left_img = cv2.imread(left_img_path,0)
        cv2.imwrite(os.path.join(save_path, str(i)) +'.png', left_img)

        for k in range(0, 5):  #字符宽度
            new_img = add_noise(copy.deepcopy(left_img))
            cv2.imwrite(os.path.join(save_path, str(i))+ '_' + str(k)+ '_' +'a.png', new_img)

            new_img1 = remove_noise(copy.deepcopy(left_img))
            cv2.imwrite(os.path.join(save_path, str(i)) + '_' + str(k) + '_' + 'r.png', new_img1)


def add_noise(img,k=5):
    new_img = copy.deepcopy(img)
    for i in range(k): #添加点噪声
        temp_x = np.random.randint(0,new_img.shape[0])
        temp_y = np.random.randint(0,new_img.shape[1])
        new_img[temp_x][temp_y] = 255
    return new_img

def remove_noise(img,k=5):
    new_img = copy.deepcopy(img)
    for i in range(k): #添加点噪声
        temp_x = np.random.randint(0,new_img.shape[0])
        temp_y = np.random.randint(0,new_img.shape[1])
        new_img[temp_x][temp_y] = 0
    return new_img
def concat(src1,src2):  #水平连接
    img = []

    for s1,s2 in zip(src1,src2):
        t = []
        for i in s1:
            t.append(i)
        for i in s2:
            t.append(i)
        img.append(t)
    return np.asarray(img,dtype=np.uint8)

def start_deal(start, end, new_start, path, save_path):   #[1 , [4 , [8
    all_imgs = []
    for i in range(start, end+1):

        left_img_path = os.path.join(path, str(i) + '_0.png')
        right_img_path = os.path.join(path, str(i) + '_1.png')
        left_img = cv2.imread(left_img_path,0)
        right_img = cv2.imread(right_img_path,0)
        # save = concat_img(left_img, right_img)
        # cv2.imwrite(os.path.join(save_path,str(i)) + '.png', save)
        diff = left_img.shape[0] - right_img.shape[0]
        for k in range(0, 3):  #字符宽度
            new_left_img = concat_img(left_img, np.zeros((left_img.shape[0],k), dtype=np.uint8))

            # num = 6
            # if new_img.shape[0] == right_img.shape[0]:
            #     num=2
            if diff >=4:
                for n in range(-2, 3, 2):
                    new_img = add_v(right_img, diff // 2 + n, False)
                    save_img = add_v(new_img, diff // 2 - n, True)


                    save = concat_img(new_left_img, save_img)

                    si = add_noise(save, 5)
                    cv2.imwrite(os.path.join(save_path, str(i)) + '_' + str(k) + '_' + str(n + 2) + 'a.png', si)
                    si = remove_noise(save, 5)
                    cv2.imwrite(os.path.join(save_path, str(i))+ '_' + str(k)+ '_' + str(n+2) + 'r.png', si)


            else :
                for n in range(-1, 2, 1):
                    new_img = add_v(right_img, diff // 2 + n, False)
                    save_img = add_v(new_img, diff // 2 - n, True)

                    save = concat_img(new_left_img, save_img)

                    si = add_noise(save, 5)
                    cv2.imwrite(os.path.join(save_path, str(i)) + '_' + str(k) + '_' + str(n + 2) + 'a.png', si)
                    si = remove_noise(save, 5)
                    cv2.imwrite(os.path.join(save_path, str(i)) + '_' + str(k) + '_' + str(n + 2) + 'r.png', si)
def concat_img_2(src1, src2, std_diff=1):  #将第二个图片置为中间位置以及上下浮动
    diff = src1.shape[0] - src2.shape[0]
    s1 = np.asarray(src1, dtype=np.uint8).tolist()
    s2 = np.asarray(src2, dtype=np.uint8).tolist()

    if diff == 0:
        return concat(src1, src2)
    elif diff > 0:
        new_s2 = []

        for n in range(-2, 3, 4):
            new_img = add_v(s2, diff // 2 + n, False)
            save_img = add_v(new_img, diff // 2 - n, True)
            # cv2.imwrite(os.path.join(save_path, str(i)) + '_' + str(k) + '_' + str(n) + '.png', concat(s1, save_img))

        return concat(s1, new_img)
    else:
        new_s1 = []
        diff = 0 - diff
        for k in range(diff - std_diff + 1):
            t_l = []
            for i in range(len(s1[0])):
                t_l.append(0)
            new_s1.append(t_l)

        for k in range(len(s1)):
            new_s1.append(s1[k])

        for k in range(std_diff - 1):
            t_l = []
            for i in range(len(s1[0])):
                t_l.append(0)
            new_s1.append(t_l)

        new_img = np.asarray(new_s1, dtype=np.uint8)
        return concat(new_img, s2)



def concat_img(src1,src2,std_diff=1):
    diff = src1.shape[0]- src2.shape[0]
    s1 = np.asarray(src1,dtype=np.uint8).tolist()
    s2 = np.asarray(src2,dtype=np.uint8).tolist()

    if diff == 0:
        return concat(src1,src2)
    elif diff >0:
        new_s2 = []
        for k in range(diff - std_diff +1):
            t_l = []
            for i in range(len(s2[0])):
                t_l.append(0)
            new_s2.append(t_l)

        for k in range(len(s2)):
            new_s2.append(s2[k])

        for k in range(std_diff -1):
            t_l = []
            for i in range(len(s2[0])):
                t_l.append(0)
            new_s2.append(t_l)


        new_img = np.asarray(new_s2, dtype=np.uint8)
        return concat(s1, new_img)
    else:
        new_s1 = []
        diff = 0 - diff
        for k in range(diff - std_diff +1):
            t_l = []
            for i in range(len(s1[0])):
                t_l.append(0)
            new_s1.append(t_l)

        for k in range(len(s1)):
            new_s1.append(s1[k])

        for k in range(std_diff -1):
            t_l = []
            for i in range(len(s1[0])):
                t_l.append(0)
            new_s1.append(t_l)

        new_img = np.asarray(new_s1, dtype=np.uint8)
        return concat(new_img, s2)

def make_dir(start, end, path):
    for i in range(start,end+1):
        pa = os.path.join(path,str(i))
        os.makedirs(pa)

def gent_char():
    num = '0123456789'
    apl = '，。、.‘’“”：？·+-=×÷*#'
    with open('label5_last.txt','w') as f:
        for i in range(len(num)):
            c = 'I' + num[i]
            f.write(c + '\n')
            f.write(c[::-1] + '\n')

        for i in range(len(apl)):
            c = 'I' + apl[i]
            f.write(c + '\n')
            f.write(c[::-1] + '\n')
        
        for i in range(len(num)):
            c = '1' + num[i]
            f.write(c + '\n')
            f.write(c[::-1] + '\n')

        for i in range(len(apl)):
            c = '1' + apl[i]
            f.write(c + '\n')
            f.write(c[::-1] + '\n')


def make_border(start, end, new_start, path, save_path,height):

    for i in range(start, end+1):

        left_img_path = os.path.join(path, str(i) + '_0.png')

        left_img = cv2.imread(left_img_path,0)
        # left_img = cv2.resize(left_img,(8,8))
        for k in range(32, 44 +1, 4):  #字符宽度  5个尺度

            rand = [-3, -2,-1,-1,0,0,1,1,2,3]
            for s in range(2):
                n = random.choice(rand)
                new_img = add_v(left_img, (k - left_img.shape[0] ) // 2  + n, False)
                save_img = add_v(new_img, (k - left_img.shape[0] ) // 2  - n, True)


                cv2.imwrite(os.path.join(save_path, str(i))+ '_' + str(k)+ '_' + str(n) + '.png', save_img)

def add_v(src1,length, top=True):


    s1 = np.asarray(src1, dtype=np.uint8).tolist()

    if top:
        new_s1 = []

        for k in range(len(s1)):
            new_s1.append(s1[k])
        for k in range(length):
            t_l = []
            for i in range(len(s1[0])):
                t_l.append(0)
            new_s1.append(t_l)

    else :
        new_s1 = []

        for k in range(length):
            t_l = []
            for i in range(len(s1[0])):
                t_l.append(0)
            new_s1.append(t_l)

        for k in range(len(s1)):
            new_s1.append(s1[k])


    return np.asarray(new_s1, dtype=np.uint8)

    # test_images_dir = save_path.replace('train', 'test')
    # train_images_dir = save_path
    # np.random.shuffle(all_imgs)
    # test_num = len(all_imgs) * 0.2
    # count = 0
    # for i in range(len(all_imgs)):
    #
    #     if count < test_num :
    #         char_dir = os.path.join(test_images_dir, str(new_start))
    #     else:
    #         char_dir = os.path.join(train_images_dir, str(new_start))
    #
    #     if not os.path.isdir(char_dir):
    #         os.makedirs(char_dir)
    #
    #     path_image = os.path.join(char_dir, "%d.png" % count)
    #     cv2.imwrite(path_image, all_imgs[i])
    #     count +=1

def dilate_top_down_img_fixed(start, end, new_start, path, save_path,):

    for i in range(start, end+1):

        left_img_path = os.path.join(path, str(i) + '_0.png')

        left_img = cv2.imread(left_img_path,0)

        for s in range(0,3,1):
            if (s<6):
                new_img = add_v(left_img, s, False)
                save_img = add_v(new_img, s, True)

                si = add_noise(save_img,5)
                cv2.imwrite(os.path.join(save_path, str(i) + '_' + str(s) + '_2a.png'), si)
                si = remove_noise(save_img,5)
                cv2.imwrite(os.path.join(save_path, str(i) + '_' + str(s) + '_2r.png'), si)

            new_img = add_v(left_img, s, False)
            save_img = add_v(new_img, 1, True)
            si = add_noise(save_img, 5)
            cv2.imwrite(os.path.join(save_path, str(i) + '_' + str(s) + '_1a.png'), si)
            si = remove_noise(save_img, 5)
            cv2.imwrite(os.path.join(save_path, str(i) + '_' + str(s) + '_1r.png'), si)

            new_img = add_v(left_img, 2, False)
            save_img = add_v(new_img, s, True)
            si = add_noise(save_img, 5)
            cv2.imwrite(os.path.join(save_path, str(i) + '_' + str(s) + '_0a.png'), si)
            si = remove_noise(save_img, 5)
            cv2.imwrite(os.path.join(save_path, str(i) + '_' + str(s) + '_0r.png'), si)

def dilate_top_down_img_fixed_speifiy(start, end, new_start, path, save_path,):  # mkh

    for i in range(start, end+1):

        left_img_path = os.path.join(path, str(i) + '_0.png')

        left_img = cv2.imread(left_img_path,0)

        for s in range(0,14+1,3):
            if (s<=6):
                new_img = add_v(left_img, s, False)
                save_img = add_v(new_img, s, True)

                si = add_noise(save_img,5)
                cv2.imwrite(os.path.join(save_path, str(i) + '_' + str(s) + '_2a.png'), si)
                si = remove_noise(save_img,5)
                cv2.imwrite(os.path.join(save_path, str(i) + '_' + str(s) + '_2r.png'), si)

            new_img = add_v(left_img, s, False)
            save_img = add_v(new_img, 1, True)
            si = add_noise(save_img, 5)
            cv2.imwrite(os.path.join(save_path, str(i) + '_' + str(s) + '_1a.png'), si)
            si = remove_noise(save_img, 5)
            cv2.imwrite(os.path.join(save_path, str(i) + '_' + str(s) + '_1r.png'), si)

            if (s <= 6):
                new_img = add_v(left_img, 1, False)
                save_img = add_v(new_img, s, True)
                si = add_noise(save_img, 5)
                cv2.imwrite(os.path.join(save_path, str(i) + '_' + str(s) + '_0a.png'), si)
                si = remove_noise(save_img, 5)
                cv2.imwrite(os.path.join(save_path, str(i) + '_' + str(s) + '_0r.png'), si)

def dilate_top_down__img(start, end, new_start, path, save_path):  # * - + =

    for i in range(start, end+1):

        left_img_path = os.path.join(path, str(i) + '_0.png')

        left_img = cv2.imread(left_img_path,0)

        for k in range(32, 44 + 1, 4):
            diff = k - left_img.shape[0]

            if diff >= 8:

                for b in range(-4, 4+1, 2):
                    new_img = add_v(left_img, diff // 2 + b, False)
                    save_img = add_v(new_img, diff // 2 - b, True)
                    cv2.imwrite(os.path.join(save_path, str(i) + '_' + str(k) + '_' + str(b) + '.png'), save_img)

            else:
                d = diff // 2
                for b in range(-d, d+1, 2):
                    new_img = add_v(left_img, diff // 2 + b, False)
                    save_img = add_v(new_img, diff // 2 - b, True)
                    cv2.imwrite(os.path.join(save_path, str(i) + '_' + str(k) + str(b) + '_' + '1.png'), save_img)

                for p in range(0,(8-diff) // 4):
                    si = add_noise(left_img, 6)
                    cv2.imwrite(os.path.join(save_path, str(i) + '_' + str(k) + str(p) + '_' + '2.png'), si)
                    si = remove_noise(left_img, 6)
                    cv2.imwrite(os.path.join(save_path, str(i) + '_' + str(k) + str(p) + '_' + '3.png'), si)


        # new_img = add_v(left_img,  2, False)
        # save_img = add_v(new_img,  2, True)
        #
        # cv2.imwrite(os.path.join(save_path, str(i)) + '_' + '2.png', save_img)
        # cv2.imwrite(os.path.join(save_path, str(i)) + '_ori_' + '2.png', left_img)



        # for k in range(32, 44 +1, 4):  #字符宽度  5个尺度
        #
        #     rand = [-3, -2,-1,-1,0,0,1,1,2,3]
        #     for s in range(2):
        #         n = random.choice(rand)
        #         new_img = add_v(left_img, (k - left_img.shape[0] ) // 2  + n, False)
        #         save_img = add_v(new_img, (k - left_img.shape[0] ) // 2  - n, True)
        #
        #
        #         cv2.imwrite(os.path.join(save_path, str(i))+ '_' + str(k)+ '_' + str(n) + '.png', save_img)
if __name__ == '__main__':

    path0 = r'C:\Users\vanlance\Desktop\char\char\1\train'
    new_path = r'C:\Users\vanlance\Desktop\char\save\1'
    make_dir(0, 120, new_path)
    start = 0
    end = 29
    new_start = 4500
    height = 35
    count = 0
    for p in range(0,120 +1):
        path_ = os.path.join(path0, str(p))
        new_p = os.path.join(new_path, str(p))
        start_deal_s_t(start, end, new_start + count, path_, new_p)


        count += 1




    # gent_char()
